//
//  UIScrollView+YHNoData.m
//  HonourJJ
//
//  Created byYH003 on 2018/7/27.
//  Copyright © 2018年 Honour_iOS. All rights reserved.
//

#import "UIScrollView+YHNoData.h"

#import "UIFont+YH.h"

#import <objc/runtime.h>
#import "NSMutableAttributedString+YH.h"
#import <YHConstant/YHConstant+Method.h>
#import "UIColor+YHStyle.h"
#import "YHDefines.h"
#import "UIButton+YH.h"
#import <Masonry/Masonry.h>

@implementation UIScrollView (YHNoData)




+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        SEL originalSelector = @selector(didMoveToSuperview);
        SEL swizzledSelector = @selector(yh_didMoveToSuperview);
        
        yh_swizzleMethod([self class],originalSelector,swizzledSelector);
    });
}



#pragma mark - Method Swizzling

- (void)yh_didMoveToSuperview
{
    NSBundle * bundleClass = [NSBundle bundleForClass:[self class]];
    
    if(bundleClass == [NSBundle mainBundle] ||
       [self isMemberOfClass:[UITableView class]] ||
       [self isMemberOfClass:[UICollectionView class]]){
        if(!self.emptyDelegate){
            [self emptyConfig:nil];
        }
    }
    
    [self yh_didMoveToSuperview];
}




//- (void)didMoveToSuperview;

- (YHDZNEmptyDelegate *)emptyDelegate{
    return objc_getAssociatedObject(self, @selector(emptyDelegate));
}
-(void)setEmptyDelegate:(YHDZNEmptyDelegate *)emptyDelegate{
    objc_setAssociatedObject(self, @selector(emptyDelegate), emptyDelegate, OBJC_ASSOCIATION_RETAIN);
}

-(void)emptyConfig:(void (^)(YHDZNEmptyDelegate *))configBlock{
    
    YHDZNEmptyDelegate *delegate = [YHDZNEmptyDelegate new];
    delegate.spaceHeight = 13;
    delegate.titleColor = [UIColor yh_hint];
    delegate.titleFont = [UIFont yh_pflOfSize:14];
    //    delegate.imageUrlString = [YHStyleConfigHelper shareManager].nodata_imgName;
    delegate.title = LS(@"暂无数据");
    delegate.imageUrlString = @"nodata_nor";
    
    [self noNetworkConfig:delegate];
    if(configBlock){
        configBlock(delegate);
    }
    
    
    self.emptyDataSetSource = delegate;
    self.emptyDataSetDelegate = delegate;
    
    self.emptyDelegate = delegate;
}

- (void)emptyBtnConfig:(void (^)(UIButton * _Nonnull))btnConfig
         btnClickBlock:(void (^)(void))btnClickBlock{
    
    YHDZNEmptyDelegate *delegate = [YHDZNEmptyDelegate new];
//    delegate.spaceHeight = Adapted(24);
    
    UIButton * btnView = [UIButton buttonWithType:UIButtonTypeCustom];
    btnView.titleLabel.font = [UIFont yh_pfOfSize:14];
    [btnView setTitleColor:[UIColor yh_hint] forState:UIControlStateNormal];
    [btnView setBackgroundColor:[UIColor yh_background]];
    btnView.layer.cornerRadius = 4;
    btnView.layer.masksToBounds = YES;
    btnView.titleLabel.numberOfLines = 0;
    btnView.contentEdgeInsets = UIEdgeInsetsMake(5, 15, 5, 15);
    [btnView setTitle:@"暂无数据" forState:UIControlStateNormal];
    [btnView addActionHandler:^(UIButton *sender, NSInteger tag) {
        if(btnClickBlock){
            btnClickBlock();
        }
    }];
    
    if(btnConfig){
        btnConfig(btnView);
    }
    
    UIView * customView = [UIView new];
    [customView addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(customView);
        make.top.bottom.equalTo(customView);
    }];
    delegate.customView = customView;
    
    self.emptyDataSetSource = delegate;
    self.emptyDataSetDelegate = delegate;
    
    self.emptyDelegate = delegate;
}


//无网络判断
- (void)noNetworkConfig:(YHDZNEmptyDelegate *)delegate{
    
//    delegate.noNetworkAttTilte = [NSMutableAttributedString yh_initWithStr:LS(@"网络异常") customBlock:^(NSMutableAttributedString *att) {
//        [att yh_font:[UIFont yh_pflOfSize:15]];
//        [att yh_color:[UIColor yh_333333]];
//    }];
//
//    delegate.noNetworkAttSubTilte = [NSMutableAttributedString new];
//    delegate.noNetworkAttBtnTilte = [NSMutableAttributedString new];
//
//    void(^noNetworkAction)(void) = ^(void) {
//        NSLog(@"点击检验 重试网络");
//    };
//
//    [delegate setNoNetworkClickEmptyBtnCBlock:^{
//        noNetworkAction();
//    }];
//    [delegate setNoNetworkClickEmptyViewBlock:^{
//        noNetworkAction();
//    }];
}


- (void)reloadEmpty{
    [self reloadEmptyDataSet];
}

@end







@interface YHDZNEmptyDelegate()

//已经加载显示过 第一次
@property (assign, nonatomic) BOOL isLoadShow;

@end

@implementation YHDZNEmptyDelegate


#pragma mark - DZNEmptyDataSetSource

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    if (_imageUrlString) {
        return [UIImage imageNamed:self.imageUrlString];
    }
    return nil;
    
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
//    if(!APP.info.is_with_network){
//        if(self.noNetworkAttTilte){
//            return self.noNetworkAttTilte;
//        }
//    }
    
    if (self.attrTitle) {
        return self.attrTitle;
    }
    NSString *text = self.title;
    UIFont *font = self.titleFont;
    UIColor *textColor = self.titleColor;
    
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    if (!text) {
        return nil;
    }
    
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    if (paragraph) [attributes setObject:paragraph forKey:NSParagraphStyleAttributeName];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
    
    return attributedString;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
//    if(!APP.info.is_with_network){
//        if(self.noNetworkAttSubTilte){
//            return self.noNetworkAttSubTilte;
//        }
//    }
    
    NSString *text = self.subTitle;
    UIFont *font = self.subTitleFont;
    UIColor *textColor = self.subTitleColor;
    
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    if (!text) {
        return nil;
    }
    
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    if (paragraph) [attributes setObject:paragraph forKey:NSParagraphStyleAttributeName];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
    
    return attributedString;
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
    return nil;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    if(self.verticalOffset == 0){
        if([scrollView isKindOfClass:[UITableView class]]){
            UITableView * tableV = (UITableView *)scrollView;
            if(tableV.tableHeaderView){
                return CGRectGetHeight(tableV.tableHeaderView.frame)*0.5;
            }
        }
    }
    return self.verticalOffset;
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return self.spaceHeight;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

-(BOOL)emptyDataSetShouldBeForcedToDisplay:(UIScrollView *)scrollView{
    if(self.shouldShowEmpty){
        return [self.shouldShowEmpty boolValue];
    }
    return NO;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    if(self.shouldShowEmpty){
        return [self.shouldShowEmpty boolValue];
    }
    if(!self.isLoadShow){
        self.isLoadShow = YES;
        return NO;
    }
    if(CGSizeEqualToSize(scrollView.contentSize, CGSizeZero)){
        return NO;
    }
    
//    if([scrollView isKindOfClass:[UITableView class]]){
//        UITableView * tableV = (UITableView *)scrollView;
//        //        if(tableV.mj_header && tableV.mj_header.isRefreshing){
//        //            return NO;
//        //        }
//        NSInteger numberOfSections = tableV.numberOfSections;
//        for (NSInteger i = 0; i < numberOfSections; i++) {
//            if ([tableV numberOfRowsInSection:i] > 0) {
//                return NO;
//            }
//        }
//        return YES;
//    }
//    if([scrollView isKindOfClass:[UICollectionView class]]){
//        UICollectionView * collectV = (UICollectionView *)scrollView;
//        //        if(collectV.mj_header && collectV.mj_header.isRefreshing){
//        //            return NO;
//        //        }
//        NSInteger numberOfSections = collectV.numberOfSections;
//        for (NSInteger i = 0; i < numberOfSections; i++) {
//            if ([collectV numberOfItemsInSection:i] > 0) {
//                return NO;
//            }
//        }
//        return YES;
//    }
    
    return YES;
}

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
{
    if (_customView) {
        return _customView;
    }
    
    return nil;
}

#pragma mark - DZNEmptyDelegate

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
//    if(!APP.info.is_with_network){
//        if(self.noNetworkClickEmptyViewBlock){
//            self.noNetworkClickEmptyViewBlock();
//            return;
//        }
//    }
    
    if (self.clickEmptyViewBlock) {
        self.clickEmptyViewBlock();
    }
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
//    if(!APP.info.is_with_network){
//        if(self.noNetworkClickEmptyBtnCBlock){
//            self.noNetworkClickEmptyBtnCBlock();
//            return;
//        }
//    }
    
    if (self.clickEmptyBtnBlock) {
        self.clickEmptyBtnBlock();
    }
}


@end
